package com.ark.cloud.dataservices.modules.sys.controller;

import com.ark.cloud.dataservices.common.web.entity.Result;
import com.ark.cloud.dataservices.modules.sys.entity.form.UserForm;
import com.ark.cloud.dataservices.modules.sys.entity.form.UserQueryForm;
import com.ark.cloud.dataservices.modules.sys.entity.form.UserUpdateForm;
import com.ark.cloud.dataservices.modules.sys.entity.param.UserQueryParam;
import com.ark.cloud.dataservices.modules.sys.entity.po.User;
import com.ark.cloud.dataservices.modules.sys.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Set;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    private final IUserService userService;

    public UserController(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 新增用户
     *
     * @param userForm {@link UserForm}
     * @return {@link Result}
     */
    @PostMapping
    public Result<?> add(@Valid @RequestBody UserForm userForm) {
        log.debug("name:{}", userForm);
        User user = userForm.toPo(User.class);
        return Result.success(userService.add(user));
    }

    /**
     * 删除用户
     *
     * @param id 用户 id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        return Result.success(userService.delete(id));
    }

    /**
     * 修改用户
     *
     * @param id             用户 id
     * @param userUpdateForm {@link UserUpdateForm}
     * @return {@link Result}
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@PathVariable String id, @Valid @RequestBody UserUpdateForm userUpdateForm) {
        User user = userUpdateForm.toPo(User.class);
        user.setId(id);
        return Result.success(userService.update(user));
    }

    /**
     * 获取用户
     *
     * @param id 用户 id
     * @return {@link Result}
     */
    @GetMapping(value = "/{id}")
    public Result<?> get(@PathVariable String id) {
        log.debug("get with id:{}", id);
        return Result.success(userService.get(id));
    }

    /**
     * 获取用户
     *
     * @param uniqueId 用户唯一标识
     * @return {@link Result}
     */
    @GetMapping
    public Result<?> query(@RequestParam String uniqueId) {
        log.debug("query with username or mobile:{}", uniqueId);
        return Result.success(userService.getByUniqueId(uniqueId));
    }

    /**
     * 搜索用户
     *
     * @param userQueryForm {@link UserQueryForm}
     * @return {@link Result}
     */
    @PostMapping(value = "/conditions")
    public Result<?> search(@Valid @RequestBody UserQueryForm userQueryForm) {
        log.debug("search with userQueryForm:{}", userQueryForm);
        return Result.success(userService.query(userQueryForm.getPage(), userQueryForm.toParam(UserQueryParam.class)));
    }

    @PostMapping("/list")
    public Result<?> list() {
        log.debug("query all users");
        return Result.success(userService.all());
    }

    @PutMapping(value = "/seats/{id}")
    public Result<?> addSeatsToUser(@PathVariable String id, @RequestBody Set<String> seatIds) {
        log.debug("save Batch {}", id);
        return Result.success(userService.addSeatsToUserBySeatIds(id, seatIds));
    }

}